<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.14"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>FabGL: NetworkTerminal/NetworkTerminal.ino</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(initResizable);
/* @license-end */</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">FabGL
   </div>
   <div id="projectbrief">ESP32 VGA Controller and Graphics Library</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.14 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('_network_terminal_2_network_terminal_8ino-example.html','');});
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">NetworkTerminal/NetworkTerminal.ino</div>  </div>
</div><!--header-->
<div class="contents">
<p>Network VT/ANSI Terminal</p>
<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - www.fabgl.com</span></div><div class="line"><span class="comment">  Copyright (c) 2019 Fabrizio Di Vittorio.</span></div><div class="line"><span class="comment">  All rights reserved.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  This file is part of FabGL Library.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  FabGL is free software: you can redistribute it and/or modify</span></div><div class="line"><span class="comment">  it under the terms of the GNU General Public License as published by</span></div><div class="line"><span class="comment">  the Free Software Foundation, either version 3 of the License, or</span></div><div class="line"><span class="comment">  (at your option) any later version.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  FabGL is distributed in the hope that it will be useful,</span></div><div class="line"><span class="comment">  but WITHOUT ANY WARRANTY; without even the implied warranty of</span></div><div class="line"><span class="comment">  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span></div><div class="line"><span class="comment">  GNU General Public License for more details.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  You should have received a copy of the GNU General Public License</span></div><div class="line"><span class="comment">  along with FabGL.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span></div><div class="line"><span class="comment"> */</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="fabgl_8h.html">fabgl.h</a>&quot;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;WiFi.h&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;ICMP.h&quot;</span></div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">char</span> <span class="keyword">const</span> * AUTOEXEC = <span class="stringliteral">&quot;info\r&quot;</span></div><div class="line">                        <span class="stringliteral">&quot;scan\r&quot;</span>;</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">enum class</span> State { Prompt, PromptInput, UnknownCommand, Help, <a class="code" href="group___enumerations.html#ggaf2f79b5a07ca9d7f44f264b12d986c65a4059b0251f66a18cb56f544728796875">Info</a>, Wifi, TelnetInit, Telnet, Scan, Ping, Reset };</div><div class="line"></div><div class="line"></div><div class="line">State        state = State::Prompt;</div><div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span>    INPUTLINESIZE = 80;</div><div class="line"><span class="keywordtype">char</span>         inputLine[INPUTLINESIZE + 1];</div><div class="line"><span class="keywordtype">int</span>          inputPos = 0;</div><div class="line">WiFiClient   client;</div><div class="line"><span class="keywordtype">char</span> <span class="keyword">const</span> * currentScript = <span class="keyword">nullptr</span>;</div><div class="line"><span class="keywordtype">bool</span>         error = <span class="keyword">false</span>;</div><div class="line"></div><div class="line">TerminalClass Terminal;</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> exe_info()</div><div class="line">{</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[37m* * FabGL - Network VT/ANSI Terminal\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[34m* * 2019 by Fabrizio Di Vittorio - www.fabgl.com\e[32m\r\n\n&quot;</span>);</div><div class="line">  Terminal.printf(<span class="stringliteral">&quot;\e[32mScreen Size        :\e[33m %d x %d\r\n&quot;</span>, VGAController.<a name="a0"></a><a class="code" href="classfabgl_1_1_v_g_a_controller_class.html#ad7310c48604f37aec2ea3274fefd405c">getScreenWidth</a>(), VGAController.<a name="a1"></a><a class="code" href="classfabgl_1_1_v_g_a_controller_class.html#af3406190a30fc46070da0a7353da4082">getScreenHeight</a>());</div><div class="line">  Terminal.printf(<span class="stringliteral">&quot;\e[32mTerminal Size      :\e[33m %d x %d\r\n&quot;</span>, Terminal.getColumns(), Terminal.getRows());</div><div class="line">  Terminal.printf(<span class="stringliteral">&quot;\e[32mKeyboard           :\e[33m %s\r\n&quot;</span>, Keyboard.<a name="a2"></a><a class="code" href="classfabgl_1_1_keyboard_class.html#a5c07c13d95a52b5c8d91a7328fe17930">isKeyboardAvailable</a>() ? <span class="stringliteral">&quot;OK&quot;</span> : <span class="stringliteral">&quot;Error&quot;</span>);</div><div class="line">  Terminal.printf(<span class="stringliteral">&quot;\e[32mFree DMA Memory    :\e[33m %d\r\n&quot;</span>, heap_caps_get_free_size(MALLOC_CAP_DMA));</div><div class="line">  Terminal.printf(<span class="stringliteral">&quot;\e[32mFree 32 bit Memory :\e[33m %d\r\n&quot;</span>, heap_caps_get_free_size(MALLOC_CAP_32BIT));</div><div class="line">  <span class="keywordflow">if</span> (WiFi.status() == WL_CONNECTED) {</div><div class="line">    Terminal.printf(<span class="stringliteral">&quot;\e[32mWiFi SSID          :\e[33m %s\r\n&quot;</span>, WiFi.SSID().c_str());</div><div class="line">    Terminal.printf(<span class="stringliteral">&quot;\e[32mCurrent IP         :\e[33m %s\r\n&quot;</span>, WiFi.localIP().toString().c_str());</div><div class="line">  }</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\n\e[32mType \e[33mhelp\e[32m to print all available commands.\r\n&quot;</span>);</div><div class="line">  error = <span class="keyword">false</span>;</div><div class="line">  state = State::Prompt;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> exe_help()</div><div class="line">{</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[33mhelp\e[92m\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[37m  Shows this help.\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[33minfo\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[37m  Shows system info.\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[33mscan\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[37m  Scan for WiFi networks.\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[33mwifi [SSID PASSWORD]\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[37m  Connect to SSID using PASSWORD.\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[37m  Example:\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[37m    wifi MyWifi MyPassword\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[33mtelnet HOST [PORT]\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[37m  Open telnet session with HOST (IP or host name) using PORT.\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[37m  Example:\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[37m    telnet towel.blinkenlights.nl\e[32m\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[33mreboot\r\n&quot;</span>);</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;\e[37m  Restart the system.\e[32m\r\n&quot;</span>);</div><div class="line">  error = <span class="keyword">false</span>;</div><div class="line">  state = State::Prompt;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> decode_command(<span class="keywordtype">char</span> <span class="keyword">const</span> * inputLine)</div><div class="line">{</div><div class="line">  <span class="keywordflow">if</span> (*inputLine == 0)</div><div class="line">    state = State::Prompt;</div><div class="line">  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strncmp(inputLine, <span class="stringliteral">&quot;help&quot;</span>, 4) == 0)</div><div class="line">    state = State::Help;</div><div class="line">  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strncmp(inputLine, <span class="stringliteral">&quot;info&quot;</span>, 4) == 0)</div><div class="line">    state = State::Info;</div><div class="line">  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strncmp(inputLine, <span class="stringliteral">&quot;wifi&quot;</span>, 4) == 0)</div><div class="line">    state = State::Wifi;</div><div class="line">  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strncmp(inputLine, <span class="stringliteral">&quot;telnet&quot;</span>, 6) == 0)</div><div class="line">    state = State::TelnetInit;</div><div class="line">  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strncmp(inputLine, <span class="stringliteral">&quot;scan&quot;</span>, 4) == 0)</div><div class="line">    state = State::Scan;</div><div class="line">  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strncmp(inputLine, <span class="stringliteral">&quot;ping&quot;</span>, 4) == 0)</div><div class="line">    state = State::Ping;</div><div class="line">  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strncmp(inputLine, <span class="stringliteral">&quot;reboot&quot;</span>, 4) == 0)</div><div class="line">    state = State::Reset;</div><div class="line">  <span class="keywordflow">else</span></div><div class="line">    state = State::UnknownCommand;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> exe_prompt()</div><div class="line">{</div><div class="line">  <span class="keywordflow">if</span> (currentScript) {</div><div class="line">    <span class="comment">// process commands from script</span></div><div class="line">    <span class="keywordflow">if</span> (*currentScript == 0 || error) {</div><div class="line">      <span class="comment">// end of script, return to prompt</span></div><div class="line">      currentScript = <span class="keyword">nullptr</span>;</div><div class="line">      state = State::Prompt;</div><div class="line">    } <span class="keywordflow">else</span> {</div><div class="line">      <span class="comment">// execute current line and move to the next one</span></div><div class="line">      <span class="keywordtype">int</span> linelen = strchr(currentScript, <span class="charliteral">&#39;\r&#39;</span>) - currentScript;</div><div class="line">      memcpy(inputLine, currentScript, linelen);</div><div class="line">      inputLine[linelen] = 0;</div><div class="line">      currentScript += linelen + 1;</div><div class="line">      decode_command(inputLine);</div><div class="line">    }</div><div class="line">  } <span class="keywordflow">else</span> {</div><div class="line">    <span class="comment">// process commands from keyboard</span></div><div class="line">    Terminal.write(<span class="stringliteral">&quot;&gt;&quot;</span>);</div><div class="line">    state = State::PromptInput;</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> exe_promptInput()</div><div class="line">{</div><div class="line">  <span class="keywordflow">if</span> (Terminal.available()) {</div><div class="line">    <span class="keywordtype">char</span> c = Terminal.read();</div><div class="line">    <span class="keywordflow">switch</span> (c) {</div><div class="line">      <span class="comment">// DEL</span></div><div class="line">      <span class="keywordflow">case</span> 0x7F:</div><div class="line">        <span class="keywordflow">if</span> (inputPos) {</div><div class="line">          Terminal.write(<span class="stringliteral">&quot;\b\e[K&quot;</span>); <span class="comment">// backspace + ESC[K</span></div><div class="line">          --inputPos;</div><div class="line">        }</div><div class="line">        <span class="keywordflow">break</span>;</div><div class="line">      <span class="comment">// CR, process the line</span></div><div class="line">      <span class="keywordflow">case</span> 0x0D:</div><div class="line">        Terminal.write(<span class="stringliteral">&quot;\r\n&quot;</span>);</div><div class="line">        inputLine[inputPos] = 0;</div><div class="line">        decode_command(inputLine);</div><div class="line">        inputPos = 0;</div><div class="line">        <span class="keywordflow">break</span>;</div><div class="line">      <span class="comment">// directly printable chars</span></div><div class="line">      <span class="keywordflow">case</span> 32 ... 126:</div><div class="line">        <span class="keywordflow">if</span> (inputPos &lt; INPUTLINESIZE) {</div><div class="line">          Terminal.write(c);</div><div class="line">          inputLine[inputPos++] = c;</div><div class="line">        }</div><div class="line">        <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> exe_scan()</div><div class="line">{</div><div class="line">  <span class="keyword">static</span> <span class="keywordtype">char</span> <span class="keyword">const</span> * ENC2STR[] = { <span class="stringliteral">&quot;Open&quot;</span>, <span class="stringliteral">&quot;WEP&quot;</span>, <span class="stringliteral">&quot;WPA-PSK&quot;</span>, <span class="stringliteral">&quot;WPA2-PSK&quot;</span>, <span class="stringliteral">&quot;WPA/WPA2-PSK&quot;</span>, <span class="stringliteral">&quot;WPA-ENTERPRISE&quot;</span> };</div><div class="line">  Terminal.write(<span class="stringliteral">&quot;Scanning...&quot;</span>);</div><div class="line">  <span class="keywordtype">int</span> networksCount = WiFi.scanNetworks();</div><div class="line">  Terminal.printf(<span class="stringliteral">&quot;%d network(s) found\r\n&quot;</span>, networksCount);</div><div class="line">  <span class="keywordflow">if</span> (networksCount) {</div><div class="line">    Terminal.write   (<span class="stringliteral">&quot;\e[90m #\e[4GSSID\e[45GRSSI\e[55GCh\e[60GEncryption\e[32m\r\n&quot;</span>);</div><div class="line">    <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; networksCount; ++i)</div><div class="line">      Terminal.printf(<span class="stringliteral">&quot;\e[33m %d\e[4G%s\e[33m\e[45G%d dBm\e[55G%d\e[60G%s\e[32m\r\n&quot;</span>, i + 1, WiFi.SSID(i).c_str(), WiFi.RSSI(i), WiFi.channel(i), ENC2STR[WiFi.encryptionType(i)]);</div><div class="line">  }</div><div class="line">  error = <span class="keyword">false</span>;</div><div class="line">  state = State::Prompt;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> exe_wifi()</div><div class="line">{</div><div class="line">  <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">int</span> MAX_SSID_SIZE = 32;</div><div class="line">  <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">int</span> MAX_PSW_SIZE  = 32;</div><div class="line">  <span class="keywordtype">char</span> ssid[MAX_SSID_SIZE + 1];</div><div class="line">  <span class="keywordtype">char</span> psw[MAX_PSW_SIZE + 1] = {0};</div><div class="line">  error = <span class="keyword">true</span>;</div><div class="line">  <span class="keywordflow">if</span> (sscanf(inputLine, <span class="stringliteral">&quot;wifi %32s %32s&quot;</span>, ssid, psw) &gt;= 1) {</div><div class="line">    WiFi.begin(ssid, psw);</div><div class="line">    Terminal.write(<span class="stringliteral">&quot;Connecting WiFi...&quot;</span>);</div><div class="line">    <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 16 &amp;&amp; WiFi.status() != WL_CONNECTED; ++i) {</div><div class="line">      Terminal.write(<span class="charliteral">&#39;.&#39;</span>);</div><div class="line">      WiFi.reconnect();</div><div class="line">      delay(1000);</div><div class="line">    }</div><div class="line">    <span class="keywordflow">if</span> (WiFi.status() == WL_CONNECTED) {</div><div class="line">      Terminal.printf(<span class="stringliteral">&quot;connected to %s, IP is %s\r\n&quot;</span>, WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());</div><div class="line">      error = <span class="keyword">false</span>;</div><div class="line">    } <span class="keywordflow">else</span></div><div class="line">      Terminal.write(<span class="stringliteral">&quot;failed!\r\n&quot;</span>);</div><div class="line">  }</div><div class="line">  state = State::Prompt;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> exe_telnetInit()</div><div class="line">{</div><div class="line">  <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">int</span> MAX_HOST_SIZE = 32;</div><div class="line">  <span class="keywordtype">char</span> host[MAX_HOST_SIZE + 1];</div><div class="line">  <span class="keywordtype">int</span> port;</div><div class="line">  error = <span class="keyword">true</span>;</div><div class="line">  <span class="keywordtype">int</span> pCount = sscanf(inputLine, <span class="stringliteral">&quot;telnet %32s %d&quot;</span>, host, &amp;port);</div><div class="line">  <span class="keywordflow">if</span> (pCount &gt; 0) {</div><div class="line">    <span class="keywordflow">if</span> (pCount == 1)</div><div class="line">      port = 23;</div><div class="line">    Terminal.printf(<span class="stringliteral">&quot;Trying %s...\r\n&quot;</span>, host);</div><div class="line">    <span class="keywordflow">if</span> (client.connect(host, port)) {</div><div class="line">      Terminal.printf(<span class="stringliteral">&quot;Connected to %s\r\n&quot;</span>, host);</div><div class="line">      error = <span class="keyword">false</span>;</div><div class="line">      state = State::Telnet;</div><div class="line">    } <span class="keywordflow">else</span> {</div><div class="line">      Terminal.write(<span class="stringliteral">&quot;Unable to connect to remote host\r\n&quot;</span>);</div><div class="line">      state = State::Prompt;</div><div class="line">    }</div><div class="line">  } <span class="keywordflow">else</span> {</div><div class="line">    Terminal.write(<span class="stringliteral">&quot;Mistake\r\n&quot;</span>);</div><div class="line">    state = State::Prompt;</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> clientWaitForChar()</div><div class="line">{</div><div class="line">  <span class="comment">// not so good...:-)</span></div><div class="line">  <span class="keywordflow">while</span> (!client.available())</div><div class="line">    ;</div><div class="line">  <span class="keywordflow">return</span> client.read();</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> exe_telnet()</div><div class="line">{</div><div class="line">  <span class="comment">// process data from remote host (up to 1024 codes at the time)</span></div><div class="line">  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; client.available() &amp;&amp; i &lt; 1024; ++i) {</div><div class="line">    <span class="keywordtype">int</span> c = client.read();</div><div class="line">    <span class="keywordflow">if</span> (c == 0xFF) {</div><div class="line">      <span class="comment">// IAC (Interpret As Command)</span></div><div class="line">      uint8_t cmd = clientWaitForChar();</div><div class="line">      uint8_t opt = clientWaitForChar();</div><div class="line">      <span class="keywordflow">if</span> (cmd == 0xFD &amp;&amp; opt == 0x1F) {</div><div class="line">        <span class="comment">// DO WINDOWSIZE</span></div><div class="line">        client.write(<span class="stringliteral">&quot;\xFF\xFB\x1F&quot;</span>, 3); <span class="comment">// IAC WILL WINDOWSIZE</span></div><div class="line">        client.write(<span class="stringliteral">&quot;\xFF\xFA\x1F&quot;</span> <span class="stringliteral">&quot;\x00\x50\x00\x19&quot;</span> <span class="stringliteral">&quot;\xFF\xF0&quot;</span>, 9);  <span class="comment">// IAC SB WINDOWSIZE 0 80 0 25 IAC SE</span></div><div class="line">      } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (cmd == 0xFD &amp;&amp; opt == 0x18) {</div><div class="line">        <span class="comment">// DO TERMINALTYPE</span></div><div class="line">        client.write(<span class="stringliteral">&quot;\xFF\xFB\x18&quot;</span>, 3); <span class="comment">// IAC WILL TERMINALTYPE</span></div><div class="line">      } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (cmd == 0xFA &amp;&amp; opt == 0x18) {</div><div class="line">        <span class="comment">// SB TERMINALTYPE</span></div><div class="line">        c = clientWaitForChar();  <span class="comment">// bypass &#39;1&#39;</span></div><div class="line">        c = clientWaitForChar();  <span class="comment">// bypass IAC</span></div><div class="line">        c = clientWaitForChar();  <span class="comment">// bypass SE</span></div><div class="line">        client.write(<span class="stringliteral">&quot;\xFF\xFA\x18\x00&quot;</span> <span class="stringliteral">&quot;wsvt25&quot;</span> <span class="stringliteral">&quot;\xFF\xF0&quot;</span>, 12); <span class="comment">// IAC SB TERMINALTYPE 0 &quot;....&quot; IAC SE</span></div><div class="line">      } <span class="keywordflow">else</span> {</div><div class="line">        uint8_t pck[3] = {0xFF, 0, opt};</div><div class="line">        <span class="keywordflow">if</span> (cmd == 0xFD)  <span class="comment">// DO -&gt; WONT</span></div><div class="line">          pck[1] = 0xFC;</div><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span> (cmd == 0xFB) <span class="comment">// WILL -&gt; DO</span></div><div class="line">          pck[1] = 0xFD;</div><div class="line">        client.write(pck, 3);</div><div class="line">      }</div><div class="line">    } <span class="keywordflow">else</span> {</div><div class="line">      Terminal.write(c);</div><div class="line">    }</div><div class="line">  }</div><div class="line">  <span class="comment">// process data from terminal (keyboard)</span></div><div class="line">  <span class="keywordflow">while</span> (Terminal.available()) {</div><div class="line">    client.write( Terminal.read() );</div><div class="line">  }</div><div class="line">  <span class="comment">// return to prompt?</span></div><div class="line">  <span class="keywordflow">if</span> (!client.connected()) {</div><div class="line">    client.stop();</div><div class="line">    state = State::Prompt;</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> exe_ping()</div><div class="line">{</div><div class="line">  <span class="keywordtype">char</span> host[64];</div><div class="line">  <span class="keywordtype">int</span> pcount = sscanf(inputLine, <span class="stringliteral">&quot;ping %s&quot;</span>, host);</div><div class="line">  <span class="keywordflow">if</span> (pcount &gt; 0) {</div><div class="line">    <span class="keywordtype">int</span> sent = 0, recv = 0;</div><div class="line">    ICMP icmp;</div><div class="line">    <span class="keywordflow">while</span> (<span class="keyword">true</span>) {</div><div class="line"></div><div class="line">      <span class="comment">// CTRL-C ?</span></div><div class="line">      <span class="keywordflow">if</span> (Terminal.available() &amp;&amp; Terminal.read() == 0x03)</div><div class="line">        <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">      <span class="keywordtype">int</span> t = icmp.ping(host);</div><div class="line">      <span class="keywordflow">if</span> (t &gt;= 0) {</div><div class="line">        Terminal.printf(<span class="stringliteral">&quot;%d bytes from %s: icmp_seq=%d ttl=%d time=%.3f ms\r\n&quot;</span>, icmp.receivedBytes(), icmp.hostIP().toString().c_str(), icmp.receivedSeq(), icmp.receivedTTL(), (double)t/1000.0);</div><div class="line">        delay(1000);</div><div class="line">        ++recv;</div><div class="line">      } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (t == -2) {</div><div class="line">        Terminal.printf(<span class="stringliteral">&quot;Cannot resolve %s: Unknown host\r\n&quot;</span>, host);</div><div class="line">        <span class="keywordflow">break</span>;</div><div class="line">      } <span class="keywordflow">else</span> {</div><div class="line">        Terminal.printf(<span class="stringliteral">&quot;Request timeout for icmp_seq %d\r\n&quot;</span>, icmp.receivedSeq());</div><div class="line">      }</div><div class="line">      ++sent;</div><div class="line"></div><div class="line">    }</div><div class="line">    <span class="keywordflow">if</span> (sent &gt; 0) {</div><div class="line">      Terminal.printf(<span class="stringliteral">&quot;--- %s ping statistics ---\r\n&quot;</span>, host);</div><div class="line">      Terminal.printf(<span class="stringliteral">&quot;%d packets transmitted, %d packets received, %.1f%% packet loss\r\n&quot;</span>, sent, recv, (<span class="keywordtype">double</span>)(sent - recv) / sent * 100.0);</div><div class="line">    }</div><div class="line">  }</div><div class="line">  state = State::Prompt;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> setup()</div><div class="line">{</div><div class="line">  Serial.begin(115200); <span class="comment">// DEBUG ONLY</span></div><div class="line"></div><div class="line">  PS2Controller.<a name="a3"></a><a class="code" href="classfabgl_1_1_p_s2_controller_class.html#a7082e498e95fd5f5a4edfbe73923f397">begin</a>(PS2Preset::KeyboardPort0);</div><div class="line"></div><div class="line">  VGAController.<a name="a4"></a><a class="code" href="classfabgl_1_1_v_g_a_controller_class.html#acdc8b95372c01a76deeb4abe360d0295">begin</a>();</div><div class="line">  VGAController.<a name="a5"></a><a class="code" href="classfabgl_1_1_v_g_a_controller_class.html#ac190a844ead4bb68817c733fb0c9a5af">setResolution</a>(<a name="a6"></a><a class="code" href="fabglconf_8h.html#ac5e5d3057b7f87058c6191c87d9a1d54">VGA_640x200_70Hz</a>, 640, 200);</div><div class="line">  <span class="comment">//VGAController.shrinkScreen(5, 0);</span></div><div class="line">  <span class="comment">//VGAController.moveScreen(-1, 0);</span></div><div class="line"></div><div class="line">  <span class="comment">// this speed-up display but may generate flickering</span></div><div class="line">  VGAController.<a name="a7"></a><a class="code" href="classfabgl_1_1_v_g_a_controller_class.html#a7e0814c52bfc5eecd93fbb5ebce8d423">enableBackgroundPrimitiveExecution</a>(<span class="keyword">false</span>);</div><div class="line"></div><div class="line">  Terminal.begin();</div><div class="line">  Terminal.connectLocally();      <span class="comment">// to use Terminal.read(), available(), etc..</span></div><div class="line">  Terminal.setLogStream(Serial);  <span class="comment">// DEBUG ONLY</span></div><div class="line"></div><div class="line">  Terminal.setBackgroundColor(<a name="a8"></a><a class="code" href="group___enumerations.html#gga843f6f6094fb6666d0e726d7b0e06f3bab097ba4cce4536843fc9a93b5d7a5af0">Color::Black</a>);</div><div class="line">  Terminal.setForegroundColor(<a name="a9"></a><a class="code" href="group___enumerations.html#gga843f6f6094fb6666d0e726d7b0e06f3baf09d081210f708ef088785200c5c616e">Color::BrightGreen</a>);</div><div class="line">  Terminal.clear();</div><div class="line"></div><div class="line">  <span class="comment">// just to align the screen</span></div><div class="line">  <span class="comment">//Terminal.write(&quot;1234567890123456789012345678901234567890123456789012345678901234567890123456789X\r\n&quot;);</span></div><div class="line"></div><div class="line">  Terminal.enableCursor(<span class="keyword">true</span>);</div><div class="line"></div><div class="line">  currentScript = AUTOEXEC;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> loop()</div><div class="line">{</div><div class="line">  <span class="keywordflow">switch</span> (state) {</div><div class="line"></div><div class="line">    <span class="keywordflow">case</span> State::Prompt:</div><div class="line">      exe_prompt();</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">    <span class="keywordflow">case</span> State::PromptInput:</div><div class="line">      exe_promptInput();</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">    <span class="keywordflow">case</span> State::Help:</div><div class="line">      exe_help();</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">    <span class="keywordflow">case</span> State::Info:</div><div class="line">      exe_info();</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">    <span class="keywordflow">case</span> State::Wifi:</div><div class="line">      exe_wifi();</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">    <span class="keywordflow">case</span> State::TelnetInit:</div><div class="line">      exe_telnetInit();</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">    <span class="keywordflow">case</span> State::Telnet:</div><div class="line">      exe_telnet();</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">    <span class="keywordflow">case</span> State::Scan:</div><div class="line">      exe_scan();</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">    <span class="keywordflow">case</span> State::Ping:</div><div class="line">      exe_ping();</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">    <span class="keywordflow">case</span> State::Reset:</div><div class="line">      ESP.restart();</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">    <span class="keywordflow">case</span> State::UnknownCommand:</div><div class="line">      Terminal.write(<span class="stringliteral">&quot;\r\nMistake\r\n&quot;</span>);</div><div class="line">      state = State::Prompt;</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">    <span class="keywordflow">default</span>:</div><div class="line">      Terminal.write(<span class="stringliteral">&quot;\r\nNot Implemeted\r\n&quot;</span>);</div><div class="line">      state = State::Prompt;</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">  }</div><div class="line">}</div><div class="line"></div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.14 </li>
  </ul>
</div>
</body>
</html>
